package com.yg.controller;


import com.yg.config.FilePathConfig;
import com.yg.config.result.R;
import com.yg.service.TeachingPracticeBaseService;
import com.yg.utils.CloseStream;
import com.yg.utils.DownLoadUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * <p>
 * 对应sheet16，此表是教学实践基地表 前端控制器
 * </p>
 *
 * @author YingGu
 * @since 2020-11-04
 */
@RestController
@RequestMapping("/teachingPracticeBase")
@CrossOrigin
public class TeachingPracticeBaseController {

    @Autowired
    private TeachingPracticeBaseService teachingPracticeBaseService;

    @Autowired
    private FilePathConfig filePathConfig;
    /**
     * 读取excel写入数据库
     * clr
     *
     * @param file
     * @return
     */
    @PostMapping("/readExcel")
    public R readExcel( MultipartFile file) {
        //获取fileInputStream
        FileInputStream fileInputStream = null;
        R r = null;
        try {
            fileInputStream = (FileInputStream) file.getInputStream();
            r = teachingPracticeBaseService.readExcel(fileInputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭fileInputStream
            CloseStream.closeFileInputStream(fileInputStream);
        }
        return r;
    }

    /**
     * 导出excel
     * clr
     *
     * @return
     */
    @GetMapping("/writeExcel")
    public R writeExcel(HttpServletResponse response,String name) throws IOException {
        R r = teachingPracticeBaseService.writeExcel(name,response);
        return r;
    }

    /**
     * 查询所有
     * clr
     *
     * @return
     */
    @GetMapping("/getTeachingPracticeBase")
    public R selectAllTeacherInfo(@Param("current") int current,@Param("size") int size) {
        R r = teachingPracticeBaseService.selectAllBaseInfo(current,size);
        return r;
    }

    /**
     *  条件查询
     * @return
     */
    @PostMapping("/selectTeachingPracticeBaseByCondition")
    public R teacherConditionalQuery(@RequestBody String teacherCondition,int current,int size){
//        System.out.println("--------------------------" + teacherCondition);
        String s = "{'name':'氨基酸'}";
        System.out.println(current);
        System.out.println(size);
        R r = teachingPracticeBaseService.baseConditionalQuery(teacherCondition,current,size);
        return r;
    }

    /**
     * 根据id更新信息
     * @return
     */
    @PutMapping("/putTeachingPracticeBase")
    public R updateBaseInfo(@RequestBody String baseJson){
        R r = teachingPracticeBaseService.updateBaseInfo(baseJson);
        return r;
    }

    /**
     * 根据id删除信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteTeachingPracticeBase")
    public R deleteTeacherById(@RequestBody String id){
        System.out.println("---------------------"+id);
        R r = teachingPracticeBaseService.deleteBaseById(id);
        return r;
    }

    /**
     * 增加操作
     */
    @PostMapping("/postTeachingPracticeBase")
    public R insertBase(@RequestBody String teachingPracticeBase){
        R r=teachingPracticeBaseService.insertBase(teachingPracticeBase);
        return r;
    }







}

